<?php
/*
 * @Description  : 日志中间件
 * @Author       : https://github.com/skyselang
 * @Date         : 2020-05-06
 * @LastEditTime : 2020-12-24
 */

namespace app\wx\middleware;

use Closure;
use think\facade\Request;
use think\facade\Config;
use app\admin\service\AdminLogService;
use loggin\loggin;

class AdminLog
{
    /**
     * 处理请求
     *
     * @param Request $request
     * @param Closure $next
     * @return Response
     */
    public function handle($request, Closure $next)
    {
        $create_time = date('Y-m-d H:i:s');
        $start_time = time();
        $log_visit_path = Request::pathinfo();
        $admin_user_id = Request::header(Config::get('admin.heder_id_key'), '');
        $requestParam = $request->param();
        //记录日志
        $uid = rand(11111,99999).time();
        Config::set('admin.loggin_id',$uid);
        Loggin::info($uid,'-------------------loggin start-------------------');
        Loggin::info($uid,'请求时间：'.$create_time);
        Loggin::info($uid,'请求地址：'.$log_visit_path);
        Loggin::info($uid,'请求方式：'.$request->method());
        Loggin::info($uid,'请求IP：'.$request->ip());
        Loggin::info($uid,'请求ID：'.$admin_user_id);
        Loggin::info($uid,'请求参数：'.json_encode($request->param(),JSON_UNESCAPED_UNICODE));
        $response = $next($request);
        Loggin::info($uid,'返回状态码：'.isset($response->getData()['code']) ?? '');
        Loggin::info($uid,'返回描述：'.isset($response->getData()['msg']) ?? '');
        Loggin::info($uid,'请求耗时：'.(time() - $start_time).'s');
        Loggin::info($uid,'-------------------loggin end-------------------');
        return $response;
    }
}
